package com.bigluo.controller;

import com.alibaba.fastjson.JSON;
import com.bigluo.api.BaseResponse;
import com.bigluo.api.req.QryUsrInfoReq;
import com.bigluo.api.req.SaveUsrInfoReq;
import com.bigluo.api.rsp.QryAllUsrInfoRsp;
import com.bigluo.api.rsp.QryUsrInfoRsp;
import com.bigluo.dao.domain.User;
import com.bigluo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luojun on 2017/8/30.
 */
@Controller
@RequestMapping("userService")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @RequestMapping(value="/qryUsrInfo")
    @ResponseBody
    public QryUsrInfoRsp qryUsrInfo(@Valid QryUsrInfoReq req, BindingResult result) {
        if(result.hasErrors()) {
            QryUsrInfoRsp rsp = new QryUsrInfoRsp();
            buildErrors(rsp,result);
            return rsp;
        }
        QryUsrInfoRsp rsp = userService.qryUsrInfo(req);
        return rsp;
    }
    private void buildErrors(BaseResponse rsp, Object result) {
        List<FieldError> errors = null;
        if(result instanceof BindingResult) {
            BindingResult rr = (BindingResult)result;
            errors = rr.getFieldErrors();
        }
        if(result instanceof Errors) {
            Errors er = (Errors)result;
            errors = er.getFieldErrors();
        }
        Map<String,String> errorMap = new HashMap();
        for(FieldError error : errors) {
            String code = error.getField();
            String value = error.getDefaultMessage();
            errorMap.put(code,value);
        }
        rsp.setRspCode("TMP00002");
        rsp.setRspMsg(JSON.toJSONString(errorMap));
    }
    @RequestMapping("/saveUsrInfo")
    @ResponseBody
    public BaseResponse saveUsrInfo(@Valid SaveUsrInfoReq req,Errors errors) {
        if(errors.hasErrors()) {
            BaseResponse rsp = new BaseResponse();
            buildErrors(rsp,errors);
            return rsp;
        }
        BaseResponse rsp = userService.saveUsrInfo(req);
        return rsp;
    }
    @RequestMapping("qryAllUsrInfo")
    @ResponseBody
    public QryAllUsrInfoRsp qryAllUsrInfo() {
        return userService.qryAllUsrInfo();
    }

    /**
     * 返回excel文件
     * @return
     */
    @RequestMapping("getUsrListExcel")
    public ModelAndView getUsrListExcel(){
        ModelAndView mv = new ModelAndView();
        QryAllUsrInfoRsp rsp = userService.qryAllUsrInfo();
        List<User> userList = rsp.getUserList();
        mv.setViewName("aaa");
        mv.addObject("userList",userList);
        return mv;
    }

}
